app.controller('VehicleModalCtrl', function ($scope, $modalInstance, $modal, toaster, param, ajax) {

    var inserter = new Req.POST({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });

    $scope.submitting = false;
    $scope.param = param;
    if ($scope.param.data == null) {
        $scope.param.data = {};
    }
    $scope.driver = [];
    $scope.coDriver = [];
    $scope.thirdDriver = [];

    if ($scope.param.type == 'update') {
        ajax.call(new Req.GET({
            resource: 'vehicle/rt/insurance/' + $scope.param.data.vehicleInsuranceId + '/id'
        })).then(function (result) {
            $scope.insurance = result.result;
            $scope.code = result.code;
        }, function () { });
    }

    //获取组织列表
    $scope.loadOrgList = function () {
        var promise = ajax.call(new Req.GET({
            resource: 'organization/2/list'
        }));
        promise.then(function (result) {
            $scope.orglist = result.result;
            //设置默认值
            if ($scope.param.data.org != null) {
                var id = $scope.param.data.org.id;
                var sel = 0;
                for (var i = 0; i < $scope.orglist.length; i++) {
                    sel++;
                    if ($scope.orglist[i].id == id) {
                        break;
                    }
                }

                $scope.orglist.selected = $scope.orglist[sel - 1];
            } else {
                $scope.orglist.selected = $scope.orglist[0];
            }

        }, function () { });
    };
    $scope.loadOrgList();


    //读取司机列表
    $scope.loadDriver = function () {
        if ($scope.param.type == 'insert') {
            $scope.driverId = 0;
        } else if ($scope.param.type == 'update') {
            $scope.driverId = $scope.param.data.driverId;
        }
        ajax.call(new Req.GET({
            resource: 'driver/' + $scope.driverId + '/nolist'
        })).then(function (result) {
            $scope.driver = result.result;

            if ($scope.param.type == 'update') {
                //设置默认值
                if ($scope.param.data.driverId != null) {
                    var id = $scope.param.data.driverId;
                    var sel = 0;
                    for (var i = 0; i < $scope.driver.length; i++) {
                        sel++;
                        if ($scope.driver[i].id == id) {
                            break;
                        }
                    }

                    $scope.driver.selected = $scope.driver[sel - 1];
                }
            }
        }, function () { });
    };

    //读取副驾司机列表
    $scope.loadCoDriver = function () {
        if ($scope.param.type == 'insert') {
            $scope.coDriverId = 0;
        } else if ($scope.param.type == 'update') {
            $scope.coDriverId = $scope.param.data.coDriverId;
        }
        var promise = ajax.call(new Req.GET({
            resource: 'driver/' + $scope.coDriverId + '/colist'
        }));
        promise.then(function (result) {
            $scope.coDriver = result.result;

            if ($scope.param.type == 'update') {
                if ($scope.param.data.coDriverId != 0) {
                    var id = $scope.param.data.coDriverId;
                    var sel = 0;
                    for (var i = 0; i < $scope.coDriver.length; i++) {
                        sel++;
                        if ($scope.coDriver[i].id == id) {
                            break;
                        }
                    }
                    $scope.coDriver.selected = $scope.coDriver[sel - 1];
                }
            }
        }, function () { });
    };

    //读取备驾司机列表
    $scope.loadThirdDriverList = function () {
        if ($scope.param.type == 'insert') {
            $scope.thirdDriverId = 0;
        } else if ($scope.param.type == 'update') {
            $scope.thirdDriverId = $scope.param.data.thirdDriverId;
        }
        var promise = ajax.call(new Req.GET({
            resource: 'driver/' + $scope.thirdDriverId + '/thirdlist'
        }));
        promise.then(function (result) {
            $scope.thirdDriver = result.result;

            if ($scope.param.type == 'update') {
                if ($scope.param.data.thirdDriverId != 0) {
                    var id = $scope.param.data.thirdDriverId;
                    var sel = 0;
                    for (var i = 0; i < $scope.thirdDriver.length; i++) {
                        sel++;
                        if ($scope.thirdDriver[i].id == id) {
                            break;
                        }
                    }
                    $scope.thirdDriver.selected = $scope.thirdDriver[sel - 1];
                }
            }
        }, function () { });
    };


    $scope.loadDriver();
    $scope.loadCoDriver();
    $scope.loadThirdDriverList();

    $scope.add = function () {
        $scope.type = 'insert';
        var modalInstance = $modal.open({
            templateUrl: 'template/rent/vehicle/addInsurance.html?v=3e8de1410b',
            controller: 'VehicleInsuranceModalCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "type": $scope.type
                    };
                }
            }
        });

        modalInstance.result.then(function (param) {
            inserter.fields = param.data;
            inserter.resource = 'vehicle/rt/insurance/insert';
            ajax.call(inserter).then(function (result) {
                if (result.code == 200) {
                    $scope.insurance = result.result;
                    $scope.param.data.vehicleInsuranceId=$scope.insurance.id;
                    toaster.pop('success', '成功', '新增信息成功。');
                } else {
                    toaster.pop('error', '错误', result.result);
                }
            }, function () {
                toaster.pop('error', '错误', '新增信息失败。');
            });
        });
    };

    $scope.update = function () {
        $scope.type = 'update';
        $scope.current_data = angular.copy($scope.insurance);
        var modalInstance = $modal.open({
            templateUrl: 'template/rent/vehicle/addInsurance.html?v=3e8de1410b',
            controller: 'VehicleInsuranceModalCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "data": $scope.current_data,
                        "type": $scope.type
                    };
                }
            }
        });

        modalInstance.result.then(function (param) {
            updater.fields = param.data;
            updater.id = param.data.id;
            updater.resource = 'vehicle/rt/insurance/update';
            ajax.call(updater).then(function (result) {
                if (result.code == 200) {
                    $scope.insurance = result.result;
                    toaster.pop('success', '成功', '修改信息成功。');
                } else {
                    toaster.pop('error', '错误', '修改信息失败！');
                }
            }, function () {
                toaster.pop('error', '错误', '修改信息失败！');
            });
        });
    };

    $scope.validDriver = function () {
        if ($scope.driver.selected && $scope.coDriver.selected
            && $scope.driver.selected.id == $scope.coDriver.selected.id)
            return true;
        if ($scope.driver.selected && $scope.thirdDriver.selected
            && $scope.driver.selected.id == $scope.thirdDriver.selected.id)
            return true;
        if ($scope.thirdDriver.selected && $scope.coDriver.selected
            && $scope.thirdDriver.selected.id == $scope.coDriver.selected.id)
            return true;
        return false;
    };

    $scope.ok = function () {
        if ($scope.myForm.$valid) {
            if ($scope.driver.selected == undefined) {
                toaster.pop('warning', '提示', '请选择主驾司机！');
                return;
            } else if ($scope.validDriver()) {
                toaster.pop('warning', '提示', '在同一个车辆档案中，同一个司机只能选择一次！');
                return;
            }
/*            else if ($scope.param.type == 'insert' && $scope.insurance == undefined) {
                toaster.pop('warning', '提示', '请添加一个保险！');
                return;
            }*/

            if ($scope.param.type == 'update' && $scope.param.data.remark == undefined)
                $scope.param.data.remark = "无";
            if ($scope.param.data.years == "") $scope.param.data.years = 0;
            if ($scope.param.data.mileage == "") $scope.param.data.mileage = 0;
            if ($scope.param.data.owner == undefined) $scope.param.data.owner = "";
            if ($scope.param.data.idCard == undefined) $scope.param.data.idCard = "";
            if ($scope.param.data.address == undefined) $scope.param.data.address = "";
            if ($scope.param.data.phone == undefined) $scope.param.data.phone = "";

            if ($scope.coDriver.selected != undefined) {
                $scope.param.data.coDriverId = $scope.coDriver.selected.id;
            } else {
                $scope.param.data.coDriverId = 0;
            }
            if ($scope.thirdDriver.selected != undefined) {
                $scope.param.data.thirdDriverId = $scope.thirdDriver.selected.id;
            } else {
                $scope.param.data.thirdDriverId = 0;
            }
            if($scope.param.type == 'insert' && $scope.insurance == undefined) {
                $scope.param.data.vehicleInsuranceId=0;
            }
            $scope.param.data.driverId = $scope.driver.selected.id;
            $scope.param.data.org = $scope.orglist.selected;
            $modalInstance.close($scope.param);
        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
        $scope.submitting = false;
    };
});